import axios from 'axios';
import { ElMessage } from 'element-plus'
import {useUserStore} from "@/stores/index.js";

const baseURL = 'http://localhost:8081';


const instance = axios.create({
  baseURL,
  timeout: 5000,
})

instance.interceptors.request.use(
  (config) => {
    const useStore = useUserStore()
    config.headers.token = useStore.token
    console.log(config)
    return config
  },
  (err) => {
    return Promise.reject(err)
  }
)

instance.interceptors.response.use(
  (response) => {
    if(response.data.code === 1) {
      console.log(response.data)
      return response
    }

    console.log(response.data)
    ElMessage({message: response.data.msg || '服务器异常',type: 'error'})
    return Promise.reject(response)
  },
  (err) => {
    console.log(err.message)

    return Promise.reject(err)
  }
)

export default instance
